package com.zbl.zblproject.other;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zbl.zblproject.core.entity.OrderItem;
import com.zbl.zblproject.core.entity.Result;
import com.zbl.zblproject.core.mapper.BrandMapper;
import com.zbl.zblproject.core.mapper.OrderItemMapper;
import com.zbl.zblproject.core.mapper.SupplierMapper;
import com.zbl.zblproject.core.utils.ResultUtil;
import com.zbl.zblproject.erp.entity.*;
import com.zbl.zblproject.erp.mapper.*;
import com.zbl.zblproject.other.service.UtilService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Optional;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/11/6
 */
@Api(tags = "工具接口")
@RestController
@RequestMapping("/utils")
public class UtilController {

    private static final Logger logger = LoggerFactory.getLogger(UtilController.class);

    @Autowired
    private UtilService utilService;

    @Autowired
    ErpGoodMapper erpGoodMapper;

    @Autowired
    ErpOrderItemMapper erpOrderItemMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    ErpOrderMapper erpOrderMapper;

    @Autowired
    ErpOweGoodsItemMapper erpOweGoodsItemMapper;

    @Autowired
    ErpWarehouseItemMapper erpWarehouseItemMapper;

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    SupplierMapper supplierMapper;

    @Autowired
    ErpCategoryMapper categoryMapper;


    @GetMapping("/update-all-goodsCat")
    @ApiOperation("更新所有的商品流水号")
    public Result updateAllGoodsCat(){
        List<ErpGoods> goodses = erpGoodMapper.selectList(null);
        for(ErpGoods goods : goodses){
            //todo 更新所有的goods的CommodityCode
            String cateId = categoryMapper.selectCateNumber(goods.getCatId());
            String flowCode =erpGoodMapper.selectMaxFlowNumber(cateId);
        }
        return ResultUtil.success("");
    }

    @GetMapping("/updateSuppliersId")
    @ApiOperation("更新所有orderItem表中的SupplierId")
    public Result updateSupplierId() {
        if(true) {
            return ResultUtil.error("禁止使用");
        }

        List<ErpOrderItem> items = erpOrderItemMapper.selectList(null);
        for (ErpOrderItem item : items) {
            Integer opp = Optional.ofNullable(supplierMapper.selectSupplierIdByGoodsId(item.getGoodsId())).orElse(0);
            erpOrderItemMapper.updateOrderSupplierById(item.getId(),
                    opp);
        }

        List<OrderItem> orderItems = orderItemMapper.selectList(null);
        for (OrderItem item : orderItems) {
            Integer opp = Optional.ofNullable(supplierMapper.selectSupplierIdByGoodsId(item.getGoodsId())).orElse(0);
            orderItemMapper.updateOrderItemSuppliers(item.getId(),
                    opp);
        }
        return ResultUtil.success("更新成功!");
    }

    @GetMapping("/updateOrderItem")
    @ApiOperation("更新所有的ErpOrderItem项")
    public Result updateErpOrderItem() {
        if(true){
            return ResultUtil.error("禁止使用");
        }

        List<ErpOrderItem> items = erpOrderItemMapper.selectList(null);
        for (ErpOrderItem item : items) {
            item.setOrderSn(erpOrderMapper.selectOrderSnById(item.getOrderId()));
            item.setGoodsName(erpGoodMapper.getGoodsNameByGoodsId(item.getGoodsId()));
            item.setBrandName(erpGoodMapper.getBrandNameByGoodsId(item.getGoodsId()));
            erpOrderItemMapper.updateById(item);
        }
        return ResultUtil.success("更新成功!");
    }


    @GetMapping("/updatePriceByBrand/{brandName}")
    @ApiOperation("更新所有出库的品牌名称")
    public Result updatePriceBrand(@ApiParam("品牌名称") @PathVariable String brandName) {
        int brandId = brandMapper.selectIdByBrandName(brandName);
        List<ErpGoods> goodses = erpGoodMapper.selectGoodsByBrandId(brandId);
        for (ErpGoods goods : goodses) {
            List<ErpOrderItem> items = erpOrderItemMapper.selectOutGoods(goods.getGoodsId());
            for (ErpOrderItem item : items) {
                item.setGoodsPrice(goods.getShopPrice());
                erpOrderItemMapper.updateById(item);
            }
        }
        return ResultUtil.success("成功");
    }


    @GetMapping("/cancel-erp/{orderSn}")
    @ApiOperation("出库单取消")
    public Result rollbackOut(@ApiParam("订单号") @PathVariable String orderSn) {
        try {
            utilService.cancelErpOrder(orderSn);
            return ResultUtil.success("取消成功");
        } catch (RuntimeException e) {
            return ResultUtil.error(e.getMessage());
        }
    }


    @GetMapping("/updatePrice/{orderSn}")
    @ApiOperation("更新订单的价格")
    public Result updatePrice(@ApiParam("订单号") @PathVariable String orderSn) {
        QueryWrapper<OrderItem> wrapper = new QueryWrapper<>();
        wrapper.eq("order_sn", orderSn);
        List<OrderItem> orderItems = orderItemMapper.selectList(wrapper);
        for (OrderItem orderItem : orderItems) {
            ErpGoods goods = erpGoodMapper.selectById(orderItem.getGoodsId());
            orderItem.setShopPrice(goods.getShopPrice());
            orderItemMapper.updateById(orderItem);
        }
        return ResultUtil.success("成功!");
    }


    @PostMapping("/uploadGoods")
    @ApiOperation("提交xls,goods自动更新进入数据库")
    public Result uploadGoods(@RequestParam("file") MultipartFile file) {
        try {
            utilService.updateGoodsToDatabase(file);
            return ResultUtil.success("导入成功!");
        } catch (NullPointerException e) {
            return ResultUtil.error("一些字段为空");
        } catch (RuntimeException e) {
            logger.info(e.getMessage());
            return ResultUtil.error(e.getMessage());
        }
    }

    @PostMapping("/uploadGoods2")
    @ApiOperation("提交xlsx,自动导入到数据库")
    public Result uploadGoods2(@RequestParam("file") MultipartFile file) {
        try {
            utilService.updateGoodsToDatabase2(file);
            return ResultUtil.success("导入成功!");
        } catch (NullPointerException e) {
            return ResultUtil.error("一些字段为空");
        } catch (RuntimeException e) {
            e.printStackTrace();
            logger.info(e.getMessage());
            return ResultUtil.error(e.getMessage());
        }
    }



    @GetMapping("/updateErpOrder")
    @ApiOperation("更新Erp订单信息")
    public Result updateErpOrder() {
        if(true){
            return ResultUtil.error("该接口被关闭!");
        }
        utilService.updateErpOrder();
        return ResultUtil.success("成功!");
    }





}
